package top.lixunda.ecommerce.server.user.api.generator;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.Scanner;

/**
 * @author Xumda
 */
@SuppressWarnings("Duplicates")
public class UserCodeGenerator {

    private final static String DATABASE = "user";

    private final static String PACK = "top.lixunda.ecommerce.server.user";

    private final static String PATH = "/api/atomic-api/user/src/main/temp";

    private final static String SERVER_NAME = "ATOMIC_USER";

    private static String[] tables;

    private static String scanner() {
        Scanner scanner = new Scanner(System.in);
        System.out.println(("请输入" + "表名，多个英文逗号分割" + "："));
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotEmpty(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + "表名，多个英文逗号分割" + "！");
    }

    private static void run1() {
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + PATH);
        gc.setAuthor("Lixunda");
        //创建文件后打开目录
        gc.setOpen(false);
        gc.setActiveRecord(true);
        gc.setEnableCache(true);
        gc.setBaseColumnList(true);
        gc.setBaseResultMap(true);
        //实体属性 Swagger2 注解
        gc.setSwagger2(true);

        gc.setMapperName("%sMapper")
                .setXmlName("%sMapper")
                .setServiceName("I%sService")
                .setServiceImplName("%sServiceImpl")
                .setControllerName("%sController")
                .setEntityName("%sDO");

        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://db.lixunda.top/" + DATABASE);
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("commerce");
        dsc.setPassword("Ab123456@");
        mpg.setDataSource(dsc);

        mpg.setPackageInfo(new PackageConfig().setParent(""));

        // 模板配置
        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig.setController("/myEntityController.java.vm");
        templateConfig.setMapper("/myMapper.java.vm");
        mpg.setTemplate(templateConfig);

        // 包配置
        mpg.setPackageInfo(new PackageConfig()
                .setParent(PACK)
                .setController("controller")
                .setMapper("mapper")
                .setXml("mapper.xml")
                .setEntity("entity"));

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);

        strategy.setRestControllerStyle(true);

        strategy.setInclude(tables == null ? (tables = scanner().split(",")) : tables);

        strategy.setControllerMappingHyphenStyle(true);

        strategy.setEntityTableFieldAnnotationEnable(true);
        mpg.setStrategy(strategy);

        mpg.execute();
    }

    private static void run2() {
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + PATH);
        gc.setAuthor("Xumda");
        //创建文件后打开目录
        gc.setOpen(false);
        gc.setActiveRecord(true);
        gc.setEnableCache(true);
        gc.setBaseColumnList(true);
        gc.setBaseResultMap(true);
        //实体属性 Swagger2 注解
        gc.setSwagger2(false);

        gc.setMapperName("%sMapper")
                .setXmlName("%sMapper")
                .setServiceName("Feign%sControllerFallbackFactory")
                .setServiceImplName("%sServiceImpl")
                .setControllerName("Feign%sController");

        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://db.lixunda.top/" + DATABASE);

        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("commerce");
        dsc.setPassword("Ab123456@");
        mpg.setDataSource(dsc);

        mpg.setPackageInfo(new PackageConfig().setParent(""));

        //模板配置
        mpg.setTemplate(new TemplateConfig()
                .setController("/myFeignEntityController.java.vm")
                .setEntity(null)
                .setMapper(null)
                .setXml(null)
                .setEntityKt(null)
                .setService(null)
                .setServiceImpl(null));

        // 包配置
        mpg.setPackageInfo(new PackageConfig()
                .setParent(PACK)
                .setController("feign")
                .setMapper("mapper")
                .setXml("mapper.xml")
                .setService(null)
                .setEntity("entity"));

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);

        //注意，并不是真正的提供父类，而是标注服务名称
        strategy.setSuperControllerClass(SERVER_NAME);

        strategy.setRestControllerStyle(true);

        strategy.setInclude(tables == null ? (tables = scanner().split(",")) : tables);

        strategy.setControllerMappingHyphenStyle(true);

        strategy.setEntityTableFieldAnnotationEnable(true);
        mpg.setStrategy(strategy);

        mpg.execute();
    }

    public static void main(String[] args) {
        run1();
        run2();
    }

}
